package com.cq.hd.member.controller;

import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbBusinessPointsMallApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;
import com.cq.hd.member.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商家表 前端控制器
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-21
 */
@RestController
public class TbBusinessPointsMallController implements TbBusinessPointsMallApi {

    @Autowired
    private TbBusinessPointsMallSettingsService settingsService;

    @Autowired
    private TbBusinessPointsMallProductService productService;

    @Autowired
    private TbBusinessService businessService;

    @Autowired
    private TbUserPointsAccountService userPointsAccountService;

    @Autowired
    private TbUserPointsDetailService userPointsDetailService;

    @ApiOperation(value = "获取积分规则")
    @Override
    public Result<BusinessPointsMallSettingsVo> getSettings(Long businessId) {
        return ResultGenerator.success(settingsService.getByBusinessId(businessId));
    }

    @ApiOperation(value = "设置积分规则")
    @Override
    public Result<Boolean> saveSettings(BusinessPointsMallSettingsDto businessPointsMallSettingsDto) {
        return ResultGenerator.success(settingsService.saveSettings(businessPointsMallSettingsDto));
    }

    @ApiOperation(value = "获取产品列表")
    @Override
    public Result<List<BusinessPointsMallProductVo>> getProductList(BusinessPointsMallProductListDto productListDto) {
        return ResultGenerator.success(productService.getProductList(productListDto));
    }

    @ApiOperation(value = "添加编辑优惠券")
    @Override
    public Result<Boolean> saveCoupon(BusinessPointsMallCouponDto businessPointsMallProductDto) {
        return ResultGenerator.success(productService.saveCoupon(businessPointsMallProductDto));
    }

    @ApiOperation(value = "添加编辑核销商品")
    @Override
    public Result<Boolean> saveGoods(BusinessPointsMallGoodsDto businessPointsMallGoodsDto) {
        return ResultGenerator.success(productService.saveGoods(businessPointsMallGoodsDto));
    }

    @ApiOperation(value = "删除优惠券、核销商品")
    @Override
    public Result<Boolean> removeProduct(Long id, Long businessId) {
        return ResultGenerator.success(productService.removeProduct(id, businessId));
    }

    @ApiOperation(value = "开启积分商城")
    @Override
    public Result<Boolean> open(Long businessId) {
        return ResultGenerator.success(businessService.openPointsMall(businessId));
    }

    @ApiOperation(value = "关闭积分商城")
    @Override
    public Result<Boolean> close(Long businessId) {
        return ResultGenerator.success(businessService.closePointsMall(businessId));
    }

    @ApiOperation(value = "策划人手动调整积分")
    @Override
    public Result<Boolean> businessChangePoints(@RequestBody BusinessChangePointsDto businessChangePointsDto) {
        return ResultGenerator.success(userPointsAccountService.businessChangePoints(businessChangePointsDto));
    }

    @ApiOperation(value = "用户积分列表")
    @Override
    public Result<Page<UserPointsAccountPageVo>> pageUserPointsAccount(UserPointsAccountPageDto userPointsAccountPageDto) {
        return ResultGenerator.success(userPointsAccountService.pageUserPointsAccount(userPointsAccountPageDto));
    }

    @ApiOperation(value = "用户积分详情")
    @Override
    public Result<UserPointsAccountPageVo> userPointsAccount(Long businessId, Long userId) {
        return ResultGenerator.success(userPointsAccountService.userPointsAccount(businessId, userId));
    }

    @ApiOperation(value = "用户积分明细")
    @Override
    public Result<Page<UserPointsDetailPageVo>> pageUserPointsDetail(UserPointsDetailPageDto userPointsDetailPageDto) {
        return ResultGenerator.success(userPointsDetailService.pageUserPointsDetail(userPointsDetailPageDto));
    }

    @Override
    public Result<BusinessPointsMallHomePageVo> getHomePage(Long businessId, Long userId) {
        return ResultGenerator.success(businessService.getPointsMallHomePage(businessId, userId));
    }

    @ApiOperation(value = "积分商家列表")
    @Override
    public Result<Page<BusinessPointsMallPageVo>> pageBusiness(BusinessPointsMallPageDto businessPointsMallPageDto) {
        return ResultGenerator.success(businessService.pagePointsMallBusiness(businessPointsMallPageDto));
    }

    @ApiOperation(value = "商家积分商品列表")
    @Override
    public Result<Page<BusinessPointsMallProductPageVo>> pageProducts(BusinessPointsMallProductPageDto businessPointsMallProductPageDto) {
        return ResultGenerator.success(productService.adminPageProducts(businessPointsMallProductPageDto));
    }

    @ApiOperation(value = "管理后台：用户积分明细")
    @Override
    public Result<Page<AdminUserPointsDetailPageVo>> adminPageUserPointsDetail(AdminUserPointsDetailPageDto adminUserPointsDetailPageDto) {
        return ResultGenerator.success(userPointsDetailService.adminPageUserPointsDetail(adminUserPointsDetailPageDto));
    }

    @ApiOperation(value = "用户分享赚积分")
    @Override
    public Result<Boolean> getShareActivePoints(ShareActivePointsDto shareActivePointsDto) {
        userPointsAccountService.getShareActivePoints(shareActivePointsDto);
        return ResultGenerator.success(true);
    }

    @ApiOperation(value = "商品详情")
    @Override
    public Result<BusinessPointsMallProductDetailVo> productDetail(Long id, Long userId) {
        return ResultGenerator.success(productService.productDetail(id, userId));
    }

    @ApiOperation(value = "积分兑换")
    @Override
    public Result<Boolean> exchange(Long productId, Long userId) {
        return ResultGenerator.success(userPointsAccountService.exchange(productId, userId));
    }

    // 订单结算，给用户、经纪人发放积分
    @Override
    public Result<Boolean> subOrderSettlePoints(OrderItemPointsSettleDto orderItemPointsVo) {
        return ResultGenerator.success(userPointsAccountService.subOrderSettlePoints(orderItemPointsVo));
    }

    // 积分订单取消，退回积分
    @Override
    public Result<Boolean> cancelOrderSendBackPoints(Long orderId) {
        return ResultGenerator.success(userPointsAccountService.cancelOrderSendBackPoints(orderId));
    }

    // 积分商城分享
    @Override
    public Result<BusinessPointsMallShareVo> share(Long businessId) {
        return ResultGenerator.success(businessService.sharePointsMall(businessId));
    }
}
